package com.lzj.demo.mybatis;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

@Slf4j
public class MyBatisMain {
    public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";//全局配置
        InputStream inputStream = Resources.getResourceAsStream(resource);
        //①获取数据源
        //②解析sql语句
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //③执行语句
        SqlSession sqlSession = sqlSessionFactory.openSession();
        Blog blog = sqlSession.selectOne("com.lzj.demo.mapper.BlogMapper.selectBlog",101);
        log.info("blog={}", JSONUtil.toJsonStr(blog));
    }
}
